package cn.cnlee.app.ui.home.holder;

import android.content.Context;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.adapter.holder.AbsHolder;
import com.adapter.holder.AbsItemHolder;


import java.util.ArrayList;
import java.util.List;

import cn.cnlee.app.R;
import cn.cnlee.app.adapter.CategoryAdapter;
import cn.cnlee.app.pojo.CategoryListVo;
import cn.cnlee.app.pojo.SubscriptionDTO;
import cn.cnlee.arch.util.Logger;

/**
 */
public class CategoryItemHolder extends AbsItemHolder<CategoryListVo, CategoryItemHolder.ViewHolder> {
    private List<SubscriptionDTO> list = new ArrayList<>();
    private CategoryAdapter adapter;

    public CategoryItemHolder(Context context) {
        super(context);
        adapter = new CategoryAdapter(mContext, list, R.layout.category_item_layout);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.category_layout;
    }

    @Override
    public ViewHolder createViewHolder(View view) {
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull CategoryListVo categoryTop) {
        list.clear();
        list.addAll(categoryTop.list);
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 5);
        holder.recyclerView.setLayoutManager(layoutManager);
        holder.recyclerView.setAdapter(adapter);
        holder.recyclerView.setNestedScrollingEnabled(false);
        adapter.notifyDataSetChanged();
        adapter.setOnItemClickListener((v, position) -> {
            Logger.e("-----------girdItem点击-----------");
        });
    }

    @Override
    protected void onViewAttachedToWindow(@NonNull ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        RecyclerView.LayoutParams clp = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
        if (clp instanceof StaggeredGridLayoutManager.LayoutParams) {
            ((StaggeredGridLayoutManager.LayoutParams) clp).setFullSpan(true);
        }
    }

    public static class ViewHolder extends AbsHolder {

        private RecyclerView recyclerView;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            recyclerView = getViewById(R.id.recycler_view);
        }
    }
}
